package User;

import book.Libary;
import utils.LibarySingleton;
import utils.PermissionException;
import constant.constant;
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 殇
 * Date: 2025-07-16
 * Time: 20:35
 */
public class ProxyUser {
    private User realUser;
    private Libary libary;
    //由于代理的对象可能有两种，因此使用它们的父类去接收  =》向上转型
    public ProxyUser(User realUser) {
        this.realUser = realUser;
        libary= LibarySingleton.getLibary();
    }

    public User getRealUser() {
        return realUser;
    }



    public void handelOperation(int choice){
        if(realUser instanceof AdminUser){
            switch (choice){
                case constant.SEARCH_BOOK:
                    libary.searchBook();
                    break;
                case constant.DISPLAY_BOOK:
                    libary.display();
                    break;
                case constant.EXIT:
                    libary.exit();
                    break;
                case constant.ADD_BOOK:
                    addBook();
                    break;
                case constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case constant.DELETE_BOOK:_BOOK:
                    removeBook();
                    break;
                case constant.BORROW_COUNT:
                    borrowCount();
                    break;
                case constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case constant.CHECK_STATUS:
                    checkInventoryStatus();
                    break;
                case constant.REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                    break;
            }
        }
        else if(realUser instanceof NormalUser){
            switch (choice){
                case constant.SEARCH_BOOK:
                    libary.searchBook();
                    break;
                case constant.DISPLAY_BOOK:
                    libary.display();
                    break;
                case constant.EXIT:
                    libary.exit();
                    break;
                case constant.BORROW_BOOK:
                    borrowBook();
                    break;
                case constant.RETURN_BOOK:
                    returnBook();
                    break;
                case constant.VIEW_STATUS:
                    viewBorrowHistory();
                    break;
            }
        }
    }
    //--------------------------------管理员的⽅法-----------------------------
    private void checkAdminUser(String msg){
        if(!(realUser instanceof AdminUser)){
            throw new PermissionException(msg);
        }
    }

    //调⽤菜单
    public int display() {

        return -1;
    }
    //添加书籍操作
    public void addBook() {
        checkAdminUser("您没有权限执行此操作,请更换权限");
        //realUser是User类型，必须先向下转型才能调用对应方法
        ((AdminUser)realUser).addBook();
    }
    //更新书籍操作
    public void updateBook() {
        checkAdminUser("您没有权限执行此操作,请更换权限");
        ((AdminUser)realUser).updateBook();
    }
    //移除图书
    public void removeBook() {
        checkAdminUser("您没有权限执行此操作,请更换权限");
        ((AdminUser)realUser).removeBook();
    }
    //查看每本图书的借阅次数
    public void borrowCount( ) {
        checkAdminUser("您没有权限执行此操作,请更换权限");
        ((AdminUser)realUser).borrowCount();
    }
    //查看最受欢迎的前K本书
    public void generateBook() {
        checkAdminUser("您没有权限执行此操作,请更换权限");
        ((AdminUser)realUser).generateBook();
    }
    //查看库存状态: 显示所有书籍的状态，借出or在存
    public void checkInventoryStatus() {
        checkAdminUser("您没有权限执行此操作，请更换权限");
        ((AdminUser)realUser).checkInventoryStatus();
    }
    //移除上架超过1年的书籍
    public void checkAndRemoveOldBooks() {
        checkAdminUser("您没有权限执行此操作,请更换权限");
        ((AdminUser)realUser).checkAndRemoveOldBooks();
    }


    //--------------------------------普通用户的⽅法-----------------------------
    private void checkNormalUser(String msg){
        if(!(realUser instanceof NormalUser)){
            throw new PermissionException(msg);
        }
    }
    //借阅图书
    public void borrowBook() {
        checkNormalUser("您没有权限执行此操作,请更换权限");
        ((NormalUser)realUser).borrowBook();
    }
    //归还图书
    public void returnBook() {
        checkNormalUser("您没有权限执行此操作,请更换权限");
        ((NormalUser)realUser).returnBook();
    }
    //查看个⼈借阅情况
    public void viewBorrowHistory() {
        checkNormalUser("您没有权限执行此操作,请更换权限");
        ((NormalUser)realUser).viewBorrowHistory();
    }
}
